package com.dxx.cloud.admin.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dxx.cloud.admin.module.entity.ReceiveAddress;
import com.dxx.cloud.admin.module.request.IdRequest;
import com.dxx.cloud.admin.module.request.ReceiveAddressPageRequest;
import com.dxx.cloud.admin.service.IReceiveAddressService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 37048
 */
@RestController
@RequestMapping("/receiveAddress")
@Tag(name = "收货地址模块")
@Slf4j
public class ReceiveAddressController {

    @Resource
    private IReceiveAddressService receiveAddressService;

    @Operation(summary = "添加收货地址")
    @PostMapping(value = "/add")
    public R<Boolean> add(@RequestBody @Validated ReceiveAddress request) {
        return R.ok(receiveAddressService.save(request));
    }

    @Operation(summary = "更新收货地址")
    @PostMapping(value = "/updateById")
    public R<Boolean> updateById(@RequestBody @Validated ReceiveAddress request) {
        return R.ok(receiveAddressService.updateByAddress(request));
    }

    @Operation(summary = "删除收货地址")
    @PostMapping(value = "/deleteById")
    public R<Boolean> deleteById(@RequestBody @Validated IdRequest request) {
        return R.ok(receiveAddressService.removeById(request.getId()));
    }

    @Operation(summary = "收货地址列表")
    @PostMapping(value = "/list")
    public R<List<ReceiveAddress>> list(@RequestBody @Validated ReceiveAddress request) {
        return R.ok(receiveAddressService.listByCustomerId(request.getCustomerId()));
    }

    @Schema(description = "查询收货地址列表")
    @PostMapping( "/pageList")
    public TableDataInfo<ReceiveAddress> pageList(@RequestBody @Validated ReceiveAddressPageRequest request) throws Exception {
        if (request.getPageNum() == null) {
            request.setPageNum(0);
        }
        if (request.getPageSize() == null) {
            request.setPageSize(10);
        }
        final Page<ReceiveAddress> pageRequest = new Page<>(request.getPageNum(), request.getPageSize());
        pageRequest.setOrders(List.of(OrderItem.desc("is_default")));
        Page<ReceiveAddress> page = receiveAddressService.page(pageRequest,new LambdaQueryWrapper<ReceiveAddress>().eq(null != request.getCustomerId(),ReceiveAddress::getCustomerId, request.getCustomerId()));
        return TableDataInfo.build(page);
    }

}
